package com.gm.commonlib.util.permission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;
import android.provider.Settings;

import com.gm.commonlib.util.permission.interfaces.IPermissionCallback;
import com.gm.commonlib.views.dialog.DialogUtil;

import java.util.Set;

import kr.co.namee.permissiongen.PermissionGen;

public class PermissionUtil {
    public static final String NOT_FILE_PERMISSION = "该功能缺少文件读写权限，请在权限管理中打开";
    private static String LOCATION_MSG = "App需要获取你的位置信息，确保定位相关功能正常使用，并便于物流进度跟踪。";

    public static final String NOT_CAMERA_OR_FILE_PERMISSION = "该功能缺少相机权限或者文件读写权限，请在权限管理中打开";
    public static final int RESULT_CODE = 10090;
    public static void showNotFilePermissNt(final Context context) {
        showNotPermissNt(context, NOT_FILE_PERMISSION);
    }

    //todo 拍照防止7.0以上检查url问题
    public static void checkFileUrlPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //larger than Android 4.3
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();
        }
    }


    public static void aplyBluePermission(final Context context, final IPermissionCallback callback) {
        PermissionManager.getBuilder(context)
                .apply(Manifest.permission.BLUETOOTH_CONNECT)
                .apply(Manifest.permission.BLUETOOTH_SCAN)
                .apply(Manifest.permission.ACCESS_FINE_LOCATION)
                .apply(Manifest.permission.ACCESS_COARSE_LOCATION)
                .request(new IPermissionCallback() {
                    @Override
                    public void onPermissionResult(boolean b, Set<String> set) {

                            if (callback != null) {
                                callback.onPermissionResult(b, set);
                            }

                    }
                });
    }
    public static void aplyLocationPermission(final Context context, final IPermissionCallback callback) {
        PermissionManager.getBuilder(context)
                .apply(Manifest.permission.ACCESS_FINE_LOCATION)
                .apply(Manifest.permission.ACCESS_COARSE_LOCATION)
                .request(new IPermissionCallback() {
                    @Override
                    public void onPermissionResult(boolean b, Set<String> set) {

                        if (callback != null) {
                            callback.onPermissionResult(b, set);
                        }

                    }
                });
    }

    public static void applyAudioPermission(final Context context, final IPermissionCallback callback) {
        PermissionManager.getBuilder(context)
                .apply(Manifest.permission.RECORD_AUDIO)
                .request(new IPermissionCallback() {
                    @Override
                    public void onPermissionResult(boolean b, Set<String> set) {
                        if (b) {
                            if (callback != null) {
                                callback.onPermissionResult(true, set);
                            }
                        } else {
                            if (callback==null)return;
                            callback.onPermissionResult(false, set);
//                            showNoPermissionDialog(context, LOCATION_MSG, new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialog, int which) {
//
//                                }
//                            });
                        }
                    }
                });
    }



    public static void applyCameraAndFileStorePermission(final Context context, final IPermissionCallback callback) {
        PermissionManager.getBuilder(context)
                .apply(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .apply(Manifest.permission.CAMERA)
                .request(new IPermissionCallback() {
                    @Override
                    public void onPermissionResult(boolean b, Set<String> set) {
                        if (b) {
                            if (callback != null) {
                                callback.onPermissionResult(true, set);
                            }
                        } else {
                            callback.onPermissionResult(false, set);
                            showNotPermissNt(context, NOT_CAMERA_OR_FILE_PERMISSION);
                        }
                    }
                });
    }

    public static void showNotPermissNt(final Context context, String msg) {
        showNoPermissionDialog(context, msg, null);
    }

    public static void showNoPermissionDialog(final Context context, String msg, final DialogInterface.OnClickListener listener) {
        DialogUtil.show(context, msg, "取消", "设置", null, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivity(intent);
            }
        });

    }



}
